import qs from 'qs';
import storage from '@/common/js/storage';

const install = (Vue, vm) => {
	// 此为自定义配置参数，具体参数见上方说明
	Vue.prototype.$u.http.setConfig({
	    baseUrl: 'https://www.hesongzhineng.com/whisper',
		// baseUrl: 'https://www.hesongzhineng.com/test/whisper',
	    loadingText: '努力加载中~',
	    loadingTime: 800,
	});
	
	// 请求示例
	// https://www.hesongzhineng.com/test/whisper/paopao/mini/user/getUsersInfo?c=300&y=PLAYER&uid=123456
	

    Vue.prototype.$u.http.interceptor.request = (config) => {
        const params = {
            c: 300,
            y: 'PLAYER',
            t: new Date().getTime(),
            uid: storage.get('uid') || ''
			// uid: '123456'
        };
        config.url = `${config.url}?${qs.stringify(params)}`;
        return config;
    };

    Vue.prototype.$u.http.interceptor.response = (res) => {
        if (res.code === 10000) {
            return res.data;
        } else if(res.code == 2000) {
			// 未登录的时候返回
			return {
			    status: 2000,
			    msg: res.message,
			};
		} else {
            uni.showToast({
                title: res.message,
                icon: 'none',
                duration: 3000,
            });
            return {
                status: -1,
                msg: res.message,
            };
        }
    };
};

export default {
    install,
};
